﻿using System;
using Model;

namespace ModelConsumerDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string userInput = "";
            while (userInput != "exit")
            {
                WriteMenu();
                userInput = Console.ReadLine();
                HandleUserInput(userInput);
            }
        }

        #region Properties

        private static ActualModelEntities sContext;
        private static ActualModelEntities Context
        {
            get
            {
                if (sContext == null)
                    sContext = new ActualModelEntities();
                return sContext;
            }
        }

        #endregion Properties

        private static void WriteMenu()
        {
            Console.WriteLine();
            Console.WriteLine("0 : Clear DB");
            Console.WriteLine("1 : Add pseudo FcCop rules");
            Console.WriteLine("2 : Write Rules");
        }

        private static void HandleUserInput(string userInput)
        {
            switch (userInput)
            {
                case "0":
                    ClearDB();
                    break;
                case "1":
                    AddFxCopRules();
                    break;
                case "2":
                    WriteRules();
                    break;
                default:
                    break;
            }
        }

        private static void ClearDB()
        {
            Console.Write("Clearing DB... ");
            foreach (var fxCopRule in Context.Rule_FxCopRule)
            {
                Context.DeleteObject(fxCopRule);
            }
            Context.SaveChanges();
            foreach (var rule in Context.Rules)
            {
                Context.DeleteObject(rule);
            }
            Console.WriteLine("done.");
        }

        private static void AddFxCopRules()
        {
            Console.Write("Adding pseudo FxCop rules... ");
            for (int i = 0; i < 10; i++)
            {
                Context.Rule_FxCopRule.AddObjectSafe(
                    //Context.Rule_FxCopRule.AddObject(
                    new Model.Rule_FxCopRule()
                    {
                        Category = "Category",
                        Description = "Description",
                        Email = "Email",
                        FileName = "FileName",
                        FileVersion = "FileVersion",
                        Id = "Id" + i.ToString(),
                        MessageLevelCertainty = 100,
                        MessageLevelContent = "MessageLevelContent",
                        Name = "Name",
                        Owner = "Owner",
                        TypeName = "TypeName",
                        Url = "Url"
                    });
                Context.SaveChanges();
            }
            Console.WriteLine("done.");
        }

        private static void WriteRules()
        {
            Console.WriteLine("Rules:");
            foreach (var rule in Context.Rules)
            {
                Console.WriteLine(rule.ToString());
            }
            Console.WriteLine("FxCopRules:");
            foreach (var fxCopRule in Context.Rule_FxCopRule)
            {
                Console.WriteLine(fxCopRule.ToString());
            }
        }
    }
}
